var relocation = function (selector) {
	var size = getWindowSize();
    //获取屏幕宽高  
    var scollTop = $(document).scrollTop();  
    var obj = $(selector);
    //当前窗口距离页面顶部的距离  
    var objLeft = (size.width - obj.width()) / 2;  
    ///弹出框距离左侧距离  
    var objTop = (size.height - obj.height()) / 2 + scollTop;  
    $(selector).css({ "top": objTop, "left": objLeft,"position": "fixed","_position":"absolute","text-align":"center"});
}
function getWindowSize(){
	var size = {};
	//获取窗口宽度
	if(window.innerWidth){    //兼容火狐，谷歌,safari等浏览器
		size.width=window.innerWidth;
		}
	else if((document.body)&&(document.body.clientWidth)){    //兼容IE浏览器
		size.width=document.body.clientWidth;
		}
	
	//获取窗口高度
	if(window.innerHeight){
		size.height=window.innerHeight;
	}
	else if((document.body)&&(document.body.clientHeight)){
		size.height=document.body.clientHeight;
	}
	return size;
}
function PageContext(settings){
	var _this = this; //把this保存下来，以后用_this代替this，这样就不会被this弄晕了
	this.modelFlag = settings?(settings.modelFlag?settings.modelFlag:""):"";
	this.modelName = settings?(settings.modelName?settings.modelName:""):"";
	this.modelDomainFlag = settings?(settings.modelDomainFlag?settings.modelDomainFlag:""):"";
	var defaults = {
		  operToolbarStyle:1//1：平铺,2:弹出列表
		 ,formId:'searchForm'
		 ,tableId:'searchTable'
		 ,editDivId:'edit-dialog'
		 ,addDivId:'edit-dialog'
		 ,detailDivId:'detail-dialog'
		 ,canPage:true
		 ,canUpdate:true
		 ,canDelete:true
		 ,canRemove:true
		 ,canTdRemove:true
		 ,canTdValid:true
		 ,canAdd:true
		 ,canExport:true
		 ,canImport:true
		 ,canDetail:true
		 ,top:true
		 ,scripts:null
		 ,removeUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/remove"
		 ,deleteUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/delete"
		 ,updateUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/update"
		 ,addUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/add"
		 ,saveUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/save"
		 ,toAddUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/toadd"
		 ,toUpdateUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/toupdate"
		 ,toEditUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/toedit"
		 ,toDetailUrl:ctx+"/"+_this.modelDomainFlag+"/"+_this.modelFlag+"/todetail"
	     ,indexTdindex:0
	};
	this.form = null;
	this.setting = $.extend( true,  {}, defaults, settings || {}); 
	this.gridObj = null;
    this.afterRenderGrid = function(data){
    	 var me = $('.toolbar .tableBtn');
		 var val = me.attr("val");
		 _this.controlTable(me,val);
		try {
			$('[data-rel=tooltip]').addClass("tooltip-success").poshytip();
		} catch (e) {
		}
    },
	this.formInit = function(layero,index,type){
	 };//更新后操作
	this.updateItemAfter = function(result,para){
     };
	 this.addItemAfter = function(result,para){
     };
     this.deleteItemAfter = function(id){
     };
     this.removeItemAfter = function(id){
     };
	 this.returnCheck = function(data){
		 return true;
     };
	 this.toAddCheck = function(data){
		 return true;
     };
	 this.toUpdateCheck = function(data){
		 return true;
     };
	 this.toDetailCheck = function(data){
		 return true;
     };
     this.renderAddPage = function(layero,data,name,value){
 		layero.find("input[name='"+name+"']").attr("oldval",value).val(value);
 		tvc.changeDefaultValue(layero.find("input[name='"+name+"']").get(0));
     };
     this.renderUpdatePage = function(layero,data,name,value){
		layero.find("input[name='"+name+"']").attr("oldval",value).val(value);
		tvc.changeDefaultValue(layero.find("input[name='"+name+"']").get(0));
     };
     this.renderDetailPage = function(layero,data,name,value){
    	 layero.find("span[name='"+name+"']").text(value);
     };
     this.renderAddPageOther = function(layero,data,para,option){
      };
      this.renderUpdatePageOther = function(layero,data,para,option){
      };
      this.renderDetailPageOther = function(layero,data,para,option){
      };
     this.getNowFormatDate = function () {
 	    var date = new Date();
 	    var seperator1 = "-";
 	    var seperator2 = ":";
 	    var month = date.getMonth() + 1;
 	    var strDate = date.getDate();
 	    if (month >= 1 && month <= 9) {
 	        month = "0" + month;
 	    }
 	    if (strDate >= 0 && strDate <= 9) {
 	        strDate = "0" + strDate;
 	    }
 	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
 	    return currentdate;
     } ;
     this.getLayer = function(){
    	 if(_this.setting.top){
    		 return parent.layer;
    	 }
    	 return layer;
     };
     this.init = function(){//初始化
    	 _this.initEvent();
     };
     this.renderAfterEventOther = function(){//
    	 
     };
     this.renderAfterEvent = function(){//
    	//表格-删除
		 $('#'+_this.setting.tableId+" "+'tr td ').delegate('.delBtn',"click", function (e) {
			 e.preventDefault();
			 var val = $(this).attr("val");
			 var label = $(this).attr("label");
			 if(!label || label==""){
				 label = $(this).closest("tr").attr("label");
			 }
			 _this.deleteItem(val,{label:label,success:function(result,para){
		    		_this.deleteItemAfter(val);
			 	}
			 }
			 );
			 return false;
		 }); 
		 //表格-修改
		 $('#'+_this.setting.tableId+" "+'tr td ').delegate('.updateBtn',"click", function (e) {
			 e.preventDefault();
			 var label = $(this).attr("label");
			 if(!label || label==""){
				 label = $(this).closest("tr").attr("label");
			 }
			 var id = $(this).attr("val");
			 if(_this.toUpdateCheck(id)){
			    _this.toUpdate(id,{label:label}); 
			 }
			 return false;
		 }); 
		 //表格-详情
		 $('#'+_this.setting.tableId+" "+ 'tr').delegate(" td .detailBtn","click", function (e) {
			 e.preventDefault();
			 var id = $(this).attr("val");
			 var label = $(this).attr("label");
			 if(!label || label==""){
				 label = $(this).closest("tr").attr("label");
			 }
			 if(_this.toDetailCheck(id)){
			      _this.toDetail(id,{label:label});
			 }
			 return false;
		 }); 
		 $(window).on('resize', function () {
			   var w  = document.body.clientWidth;  
			   var h  = document.body.clientHeight;  
			   if(w<=1000 && !_this.force){
				     var me = $('.toolbar .tableBtn');
					 _this.controlTable(me,"0");
			   }
		 });
     };
     this.exportData = function(me){
    	 
     }
     this.importData = function(me){
    	 
     }
     this.initEvent = function(){// 
    	 
			//搜索栏-回车事件
			 $('#'+_this.setting.formId+' input').on("keydown",function(e){
				  if(e.keyCode==13){
					 e.preventDefault();
					_this.search(); //搜索
				  }
			  });
			 //搜索栏-搜索按钮
			 $(".searchBtn").on("click",function(e){
				 _this.search();  
				 return false;
			 });
			 //搜索栏-重置按钮
			 $(".resetBtn").on("click",function(e){
				 _this.reset();  
				 return false;
			 });
			//工具栏-批量逻辑删除
			 $(".toolbar .removeBtn").on("click",function(e){
				 var ids = _this.getCheckedIds();
				 _this.removeItem(ids,{success:function(result,para){
		    			var array = ids.split(",");
		    			for (x in array)
		    			{
		    				var id = array[x];
		    			    var trObj = $("#"+_this.setting.tableId+"_"+id);
		    			    _this.setDeleteClass(trObj,true);
		    			}
			    	}});
				 return false;
			 });
			 //工具栏-批量删除
			 $(".toolbar .delBtn").on("click",function(e){
				 var ids = _this.getCheckedIds();
				 _this.deleteItem(ids,{success:function(result,para){
		    			_this.deleteItemAfter(ids);
		    	 }});
				 return false;
			 });
			 //工具栏-新增
			 $(".toolbar .addBtn").on("click",function(e){
				 e.preventDefault();
				 if(_this.toAddCheck(e)){
				    _this.toAdd(this);
				 }
				 return false;
			 });
			 //工具栏-导出
			 $(".toolbar .exportBtn").on("click",function(e){
				 _this.exportData(this)
				 return false;
			 });
			 //工具栏-导入
			 $(".toolbar .importBtn").on("click",function(e){
				 _this.importData(this)
				 return false;
			 });
			 //工具栏-返回
			 $('.returnBtn').on("click", function (e) {
				 e.preventDefault();
				 if(_this.returnCheck()){
					 var url = $(this).attr("url");
					 location = url;
				 }
				 return false;
			 }); 
			 //工具栏-表格显示控制
			 $('.toolbar .tableBtn').on("click", function (e) {
				 e.preventDefault();
				 var me = $(this);
				 var val = me.attr("val");
				 if(val=="0"){
					 val = "1";
				 }else{
					 val = "0";
				 }
				 _this.controlTable(me,val);
				 _this.force = true;
				 try {
					parent.iframeTab.iframeHeight();
				} catch (e) {
				}
				return false;
			 }); 
		    //表格的checkbox选择
			if($('#'+_this.setting.tableId+' thead tr th:eq(0) input[type=checkbox]').length == 1) {
			    $('#'+_this.setting.tableId+' thead tr th:eq(0) input[type=checkbox]').on("change",function () {
		            var checked = $.bsgrid.adaptAttrOrProp($(this), 'checked') ? true : false;
		            $.bsgrid.adaptAttrOrProp($('#'+_this.setting.tableId+' tbody tr td:nth-child(1)>input[type=checkbox]'), 'checked', checked);
		            var th_checked = this.checked;//checkbox inside "TH" table header
		    		$(this).closest('table').find('tbody > tr').each(function(){
		    			var row = this;
		    			if(th_checked) $(row).addClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', true);
		    			else $(row).removeClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', false);
		    		});
					 return false;
			    });
			}
			var active_class = 'selected selected_color';
			//select/deselect a row when the checkbox is checked/unchecked
			 $('#'+_this.setting.tableId).delegate('td input[type=checkbox]' , 'change', function(e){
				$(this).closest('table').find('tbody > tr').each(function(){
					var $row = $(this);
					var th_checked = $row.find('input[type=checkbox]').eq(0).prop('checked');
					if(th_checked) {
						$row.removeClass(active_class).addClass(active_class);
					}else{
						$row.removeClass(active_class);
					}
				});
				 return false;
			});
			//开关
			 $('#'+_this.setting.tableId).delegate('tr td .ace-switch',"click", function (e) {
				 e.preventDefault();
				 var meThis = this;
				 var doAfterChecked = $(meThis).val();//保存本次操作后的状态
				 if(doAfterChecked=="true"){
					 doAfterChecked = false;
				 }else{
					 doAfterChecked = true;
				 }
				meThis.checked = !meThis.checked;
			    var me = $(meThis) , value = me.val();
//			    var label = $(meThis).parent("label").find(".label");//显示信息
			    var name = me.attr("alias");
		    	var val = me.attr("val");//id
		    	var _label = "",label = me.closest("tr").attr("label");
		    	if(label ){
		    		_label = "【"+label+"】";
		    	}
			    if("validClass"==name){
				    var data = {};
			    	var msg = "确定标识"+_label+"为【有效】状态？";
			    	if(doAfterChecked){
			    		 data = {id:parseInt(val),validClass:1};
			    	}else{
			    		 msg = "确定标识"+_label+"为【无效】状态？";
			    		 data = {id:parseInt(val),validClass:0};
			    	}
			    	_this.updateItem(data,{msg:msg,doingmsg:"正在处理...",failMsg:"标识失败",successMsg:"标识成功",success:function(){
			    			_this.setValidClass($(meThis).closest("tr"),doAfterChecked);
			    	},closeOther:true});
			    }else if("deleteClass"==name){
			    	 var data = {};
			    	 var msg = "确定标识"+_label+"为【删除】状态？";
			    	if(!doAfterChecked){
			    		 msg = "确定标识"+_label+"为【未删除】状态？";
			    		 data = {id:parseInt(val),deleteClass:0};
			    	}else{
			    		 data = {id:parseInt(val),deleteClass:1};
			    	}
			    	_this.updateItem(data,{msg:msg,doingmsg:"正在处理...",failMsg:"标识失败",successMsg:"标识成功",success:function(){
			    			_this.setDeleteClass($(meThis).closest("tr"),doAfterChecked);
			    	},closeOther:true});
			    }else if("booleanClass"==name){
			    	var n = me.attr("name");
			    	var labelName = me.attr("label");
			    	if(labelName!="" && labelName!=undefined){
			    		labelName = "的\""+labelName+"\"";
			    	}
			    	var data = {};
			    	var msg = "确定标识"+_label+labelName+"为【是】状态？";
			    	if(doAfterChecked){
			    		 data = {id:parseInt(val)};
			    		 data[n] = 1;
			    	}else{
			    		 msg = "确定标识"+_label+labelName+"为【否】状态？";
			    		 data = {id:parseInt(val)};
			    		 data[n] = 0;
			    	}
			    	_this.updateItem(data,{msg:msg,doingmsg:"正在处理...",failMsg:"标识失败",successMsg:"标识成功",success:function(){
			    			_this.setBooleanClass($(meThis).closest("tr"),doAfterChecked,n);
			    	},closeOther:true});
		
			    }
				 return false;
			}); 
	 };
	 
	 this.controlTable = function(btn,val){
		 var th = $('#'+_this.setting.tableId+' th.nodata');
		 if(val == "0"){//隐藏
			 th.each(function(){
				 var hang = $(this).parent("tr").prevAll().length;    
				 var lie = $(this).prevAll().length;    
				 hang = Number(hang);//字符串变为数字    
				 lie = Number(lie);    
				 var target = $('#'+_this.setting.tableId+' tr').find('th:eq('+lie+')');
					 target.hide();
				 var target = $('#'+_this.setting.tableId+' tr').find('td:eq('+lie+')');
					 target.hide();
			 });
			 var cdatath = $('#'+_this.setting.tableId+' th.cdata');
			 cdatath.each(function(){
				 var hang = $(this).parent("tr").prevAll().length;    
				 var lie = $(this).prevAll().length;    
				 hang = Number(hang);//字符串变为数字    
				 lie = Number(lie);    
				 var target = $('#'+_this.setting.tableId+' tr').find('th:eq('+lie+')');
					 target.attr("width","10%");
			 });
			 btn.attr("val","0");
			 btn.find("i").text("显示");
			 $('#'+_this.setting.tableId+' tr').find(".lbl").removeClass("lbl").addClass("lbl1");
		 }else{//已显示
			 th.each(function(){
				 var hang = $(this).parent("tr").prevAll().length;    
				 var lie = $(this).prevAll().length;    
				 hang = Number(hang);//字符串变为数字    
				 lie = Number(lie);    
				 var target = $('#'+_this.setting.tableId+' tr').find('td:eq('+lie+')');
				 target.show();
				 var target = $('#'+_this.setting.tableId+' tr').find('th:eq('+lie+')');
					 target.show();
					 var w = document.body.clientWidth;  
				     if(w<=1000){	 
				    	 target.attr("width","10%");
				     }else{
				    	 var v = target.attr("w");
				    	 if(!v){
				    		 v = 120;
				    	 }
				    	 target.attr("width",v);
				     }
			 });
			 var cdatath = $('#'+_this.setting.tableId+' th.cdata');
			 cdatath.each(function(){
				 var hang = $(this).parent("tr").prevAll().length;    
				 var lie = $(this).prevAll().length;    
				 hang = Number(hang);//字符串变为数字    
				 lie = Number(lie);    
				 var target = $('#'+_this.setting.tableId+' tr').find('th:eq('+lie+')');
					 target.attr("width","120px");
			 });
			 btn.attr("val","1");
			 btn.find("i").text("隐藏");
			 $('#'+_this.setting.tableId+' tr').find(".lbl1").removeClass("lbl1").addClass("lbl");
		 }
	 }
	 
	 this.search = function(){//搜索
		 
	 };
	 this.searchCheck = function(){//搜索
		 return true;
	 };
	 this.reset = function (){//重置
	   //tvc.resetValues($("#"+_this.setting.formId).find('input,select,textarea'));
	   $("#"+_this.setting.formId).resetForm();
	   _this.resetAfter();
	 };
	 this.resetAfterForDialog = function (layero){
		 
	 }
	 this.resetAfter = function (){
		 
	 }
	 this.getCheckedIds = function() {//获取表格选中的id
	 	 
	 };
	 this.getCheckedRecords = function() {//获取表格选中的项
	     
	 };
	 this.refushOperTdClass = function(){
		 var tds = $($("#"+_this.setting.tableId+" tbody tr .operBtn")).parent('div').parent('div').parent('td');
		 tds.removeClass("lineNoWrap").addClass("lineNoWrapOper");
	  };
	 this.deleteItem = function(id,option){//删除
		 if(!id || ""==id){
	  	    _this.msg('请先选择需要删除的数据！',{closeOther:option.closeOther});
	 	  	return false;
		 }
		 var option = option || {};
		 var label = "";
		 if(option && option.label){
			 label = "【"+option.label+"】";
		 }
		 label = _this.modelName + label;
		 _this.confirm('删除后不可恢复，确定删除'+label+'？',{closeOther:option.closeOther,ok:function(){
				  option.doingmsg = '正在删除...';
		  		 
		  		  var url = _this.setting.deleteUrl;
		  		  if(option && option.url){
		  			url = option.url;
		  		  }
		 		  option.url =  url;
		 		  
		     	  var failMsg = '删除失败！';
		  		  if(option && option.failMsg){
		  			failMsg = option.failMsg;
		  		  }
		 		  option.failMsg =  failMsg;
		 		  
		     	  var successMsg = '删除成功！';
		  		  if(option && option.successMsg){
		  			successMsg = option.successMsg;
		  		  }
		 		  option.successMsg =  successMsg;
		 		 var para = {id:id,deleteClass:1};
		  		 _this.get(para,option);
			}
		});
	 };
	  this.removeItem = function(id,option){//移除
		 if(!id || ""==id){
	  	    _this.msg('请先选择需要逻辑删除的数据！',{closeOther:option.closeOther});
	 	  	return false;
		 }
		 var option = option || {};
		 var label = "";
		 if(option && option.label){
			 label = "【"+option.label+"】";
		 }
		 label = _this.modelName + label;
		_this.confirm('确定标识'+label+'为删除状态？',{closeOther:option.closeOther,ok:function(){
				 option.doingmsg = '正在处理...';
		  		 
		  		  var url = _this.setting.removeUrl;
		  		  if(option && option.url){
		  			url = option.url;
		  		  }
		 		  option.url =  url;
		 		  
		     	  var failMsg = '处理失败！';
		  		  if(option && option.failMsg){
		  			failMsg = option.failMsg;
		  		  }
		 		  option.failMsg =  failMsg;
		 		  
		     	  var successMsg = '处理成功！';
		  		  if(option && option.successMsg){
		  			successMsg = option.successMsg;
		  		  }
		 		  option.successMsg =  successMsg;
		 		  var para = {id:id};
		 		  var oldCallback = option.success;
		 		  option.success = function(result,para){
		 			  if(oldCallback){
		 				  oldCallback(result,para);
		 			  }
		 			  _this.removeItemAfter(para.id);
		 		  }
		  		  _this.get(para,option);
			}
		});
	 };
	 this.updateItem = function(para,option){//更新
		 if(!para){
	  	    _this.msg('没有需要更新的数据！',{closeOther:option.closeOther});
	 	  	return false;
		 }
		 var option = option || {};
		_this.confirm(option.msg,{closeOther:option.closeOther,ok:function(){
				  var doingmsg = '正在保存...';
		  		  if(option && option.doingmsg){
		  			doingmsg = option.doingmsg;
		  		  }
		 		  option.doingmsg =  doingmsg;
		  		  var url = _this.setting.updateUrl;
		  		  if(option && option.url){
		  			url = option.url;
		  		  }
		 		  option.url =  url;
		 		  
		     	  var failMsg = '保存失败！';
		  		  if(option && option.failMsg){
		  			failMsg = option.failMsg;
		  		  }
		 		  option.failMsg =  failMsg;
		 		  
		     	  var successMsg = '保存成功！';
		  		  if(option && option.successMsg){
		  			successMsg = option.successMsg;
		  		  }
		 		  option.successMsg =  successMsg;
		 		  
		  		  _this.post(para,option);
			}
		});
	 };
	 this.addItem = function(para,option){//更新
		return _this.saveItem(para,option);
	 }
	 this.post = function(para,option){//post
		  var option = option||{};
     	  var url = option.url;
  		  if(!url){
  			  return false;
  		  } 
  		  var failMsg = '失败！';
		  if(option && option.failMsg){
			failMsg = option.failMsg;
		  }
		  option.failMsg =  failMsg;
		  
		  var successMsg = null;
		  if(option && option.successMsg){
			successMsg = option.successMsg;
		  }
		  option.successMsg =  successMsg;
		  
		  var processing = null;
		  if(option &&  option.doingmsg){
			 option.processing = _this.getLayer().msg(option.doingmsg, {closeOther:option.closeOther,icon:1,time:4000});
		  }
		  
	    $.post(url,para,function(result){
	    	_this.doResult(option,para,result);
  	    },"json"); 
	 };
	 this.get = function(para,option){//get
		  var option = option||{};
		  var url = option.url;
  		  if(!url){
  			  return false;
  		  } 
    	  var failMsg = '失败！';
 		  if(option && option.failMsg){
 			failMsg = option.failMsg;
 		  }
 		  option.failMsg =  failMsg;
 		  
    	  var successMsg = null;
 		  if(option && option.successMsg){
 			successMsg = option.successMsg;
 		  }
 		  option.successMsg =  successMsg;
 		  
 		  var processing = null;
 		  if(option &&  option.doingmsg){
 			 option.processing = _this.getLayer().msg(option.doingmsg, {closeOther:option.closeOther,icon:1,time:4000});
 		  }
 		  
 	    $.get(url,para,function(result){
 	    	_this.doResult(option,para,result);
 	    },"json"); 
	 };
	 this.doResult = function(option,para,result){
		    var option = option || {};
		    if(option.processing){
	    		_this.getLayer().close(option.processing);
	    	}
	    	if(result){
	    		if(result.status==200){
	    			if(option && option.success)option.success(result,para);
		  	    	if(option.successMsg){
		  	    		_this.getLayer().msg(option.successMsg, {icon:1,time:2000},function(){
							//_this.getLayer().close(option.index);
						});
		  	    	}
				}else{
	    			if(result.data){
	  	    			if(result.data.globalErrors){
	    					var globalErrors = result.data.globalErrors;
	    					var len = globalErrors.length;
	    					for(var i = 0; i < len; i++ ){  
	    						var str = globalErrors[i];
	    						
	    					}
	    				}
	    				if(result.data.fieldErrors){
	    					var fieldErrors = result.data.fieldErrors;
	    					var len = fieldErrors.length;
	    					for(var i = 0; i < len; i++ ){  
	    						var obj = fieldErrors[i];
	    						
	    					}
	    				}
	    			}
	    			if(option && option.fail)option.fail(result,para);
	    			_this.alert((option.failMsg?option.failMsg:"")+result.msg, {closeOther:option.closeOther,icon:2,title:"错误信息"});
	    		}
	    	}else{
	    		if(option && option.fail)option.fail(result,para);
	    		_this.alert((option.failMsg?option.failMsg:""), {closeOther:option.closeOther,icon:2,title:"错误信息"});
	    	}
	    	
	 };

	 this.alert = function(msg,option){
	     var option = option || {};
	     option.icon = 2;
	     if(!msg){
	    	 msg = "出错啦！";
	     }
		 _this.getLayer().alert(msg, option);
		 return false;
	 } 
	 window.alert=_this.alert;
	 this.remind = function(msg,option){
	     var option = option || {};
	     option.icon = 0;
	     option.title = "提示信息";
	     if(!msg){
	    	 msg = "出错啦！";
	     }
		 _this.getLayer().alert(msg, option);
		 return false;
	 }
	 
	 this.msg = function(msg,option){
	     var option = option || {};
	     option.icon = 2;
	     option.time = 2000;
	     if(!msg){
	    	 msg = "出错啦！";
	     }
		 _this.getLayer().msg(msg, option);
		 return false;
	 }
	 this.confirm = function(msg,option,fail){
	     var option = option || {};
	     var html = '<p class="bigger-110 bolder center">'
			   +'<i class="ace-icon fa fa-exclamation-triangle blue bigger-120"></i>'
			   + msg
			   +'</p>';
	     if( typeof option === 'function' ){
	    	 _this.getLayer().confirm(html, {closeOther:option.closeOther,icon:3, title:'提示'} , function(index){
	    		 _this.getLayer().close(index);
	    		 option();
	    		 return true;
	    	 }, function(index){
	    		 if(option.cancel)option.cancel();
	    		 _this.getLayer().close(index);
	    		  if( typeof fail === 'function' ){
	    			  fail();
	    		  }
	    		 return false;
	    	 });
	     }else{
	    	 _this.getLayer().confirm(html, {closeOther:option.closeOther,icon:3, title:'提示'} , function(index){
	    		 _this.getLayer().close(index);
	    		 if(option.ok)option.ok();
	    		 return true;
	    	 }, function(index){
	    		 if(option.cancel)option.cancel();
	    		 _this.getLayer().close(index);
	    		 return false;
	    	 });
	     }
		 return false;
	 };
	 this.saveItem = function(para,option){//更新 
			 if(!para){
		  	    _this.msg('没有需要保存的数据！',{closeOther:option.closeOther});
		 	  	return false;
			 }
		     var option = option || {};
			_this.confirm(option.msg,{closeOther:option.closeOther,ok:function(){
			  		  if(!option.doingmsg){
			  			  option.doingmsg = '正在保存...';
			  		  }
			  		  var url = _this.setting.saveUrl;
			  		  if(option && option.url){
			  			url = option.url;
			  		  }
			 		  option.url =  url;
			 		  
			     	  var failMsg = '保存失败！';
			  		  if(option && option.failMsg){
			  			failMsg = option.failMsg;
			  		  }
			 		  option.failMsg =  failMsg;
			 		  
			     	  var successMsg = '保存成功！';
			  		  if(option && option.successMsg){
			  			successMsg = option.successMsg;
			  		  }
			 		  option.successMsg =  successMsg;
			 		  if(!option.success){
			 			 option.success = _this.addItemAfter;
			 		  }
			  		  _this.post(para,option);
				}
			});
		 };
		 this.getPingPuOperBtn = function (id,label,record){
				var html = [];
				html.push('<div class="hidden-sm hidden-xs btn-group">');
				var _label = "";
				if(label){
					_label = label;
				}
				if(_this.setting.canUpdate && (!record || record.canUpdate==undefined ||record.canUpdate)){
					html.push('<button class="btn btn-xs btn-info updateBtn" data-rel="tooltip" label="'+_label+'" val="'+id+'" title="修改">');
					html.push('<i class="ace-icon fa fa-pencil bigger-120"></i>');
					html.push('</button>');
				}
				
				if(_this.setting.canDelete && (!record || record.canDelete==undefined ||record.canDelete)){
					html.push('<button class="btn btn-xs btn-danger delBtn" data-rel="tooltip" label="'+_label+'" val="'+id+'"  title="删除">');
					html.push('<i class="ace-icon fa fa-trash-o bigger-120"></i>');
					html.push('</button>');
				}
				
				if(_this.setting.canDetail){
					html.push('<button class="btn btn-xs btn-success detailBtn" data-rel="tooltip" label="'+_label+'" val="'+id+'" title="详情">');
					html.push('<i class="ace-icon fa fa-search-plus bigger-120"></i>');
					html.push('</button>');
				}
				html.push(_this.getOtherPingPuOperBtn(id,label,record));
				html.push('</div>');
				return html.join("");
			};
		this.getOperBtn = function (id,label,record){
			//需去掉.bsgrid td.lineNoWrap 的overflow
			var html = [];
			html.push('<div class="hidden-md hidden-lg">');
			html.push('<div class="inline pos-rel">');
			html.push('<button class="btn btn-minier btn-primary dropdown-toggle operBtn" title="操作" data-toggle="dropdown" data-position="auto">');
			html.push('<i class="ace-icon fa fa-cog icon-only bigger-110"></i>');
			html.push('</button>');

			html.push('<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">');
			var _label = "";
			if(label){
				_label = label;
			}
			if(_this.setting.canUpdate && (!record || record.canUpdate==undefined ||record.canUpdate)){
				html.push('<li>');
				html.push('<a href="javacript:void(0)" data-rel="tooltip" label="'+_label+'" val="'+id+'" class="tooltip-success updateBtn" data-rel="tooltip" title="修改">');
				html.push('<span class="green">');
				html.push('<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>');
				html.push('</span>');
				html.push('</a>');
				html.push('</li>');
			}

			if(_this.setting.canDelete && (!record || record.canDelete==undefined ||record.canDelete)){
				html.push('<li>');
				html.push('<a href="javacript:void(0)" data-rel="tooltip" label="'+_label+'" val="'+id+'" class="tooltip-error delBtn" data-rel="tooltip" title="删除">');
				html.push('<span class="red">');
				html.push('<i class="ace-icon fa fa-trash-o bigger-120"></i>');
				html.push('</span>');
				html.push('</a>');
				html.push('</li>');
			}

			if(_this.setting.canDetail){
				html.push('<li>');
				html.push('<a href="javacript:void(0)" data-rel="tooltip" label="'+_label+'" val="'+id+'" class="tooltip-info detailBtn" data-rel="tooltip" title="详情">');
				html.push('<span class="blue">');
				html.push('<i class="ace-icon fa fa-search-plus bigger-120"></i>');
				html.push('</span>');
				html.push('</a>');
				html.push('</li>');
			}
			html.push(_this.getOtherOperBtn(id,label,record));
			html.push('</ul>');
			html.push('</div>');
			html.push('</div>');
		  return html.join("");
		};
		 //其他操作方法
		 this.getOtherOperBtn = function(id,label){
			 return "";
		 }
		 this.getOtherPingPuOperBtn = function(id,label){
			 return "";
		 }
		 this.setDeleteClass= function(trObj,value){
			 if(value==null){
				 return false;
			 }
			var meThis = trObj.find("input[name='deleteClass']");
			var me = $(meThis);
			var label = $(meThis).parent("label").find(".label");//显示信息
		    label.toggleClass("warn-label");
			if(value){
				label.removeClass("label-success").addClass("label-danger");
				label.text("已删除");
		    }else{
		    	label.removeClass("label-danger").addClass("label-success");
		    	label.text("未删除");
		    }
			meThis.checked = !value;
			me.prop("checked",!value);
			me.val(value);
		} ;
		this.setValidClass= function(trObj,value){
			 if(value==null){
				 return false;
			 }
			var meThis = trObj.find("input[name='validClass']");
			var me = $(meThis);
			var label = $(meThis).parent("label").find(".label");//显示信息
			 label.toggleClass("warn-label");
			if(value){
				label.removeClass("label-danger").addClass("label-success");
		    	label.text("有效");
		    }else{
				label.removeClass("label-success").addClass("label-danger");
    			label.text("无效");
		    }
			meThis.checked = value;
			me.prop("checked",value);
			me.val(value);
  		};
  		this.setBooleanClass= function(trObj,value,name){
			 if(value==null){
				 return false;
			 }
			var meThis = trObj.find("input[name='"+name+"']");
			var me = $(meThis);
			var label = $(meThis).parent("label").find(".label");//显示信息
			 label.toggleClass("warn-label");
			if(value){
				label.removeClass("label-danger").addClass("label-success");
		    	label.text("是");
		    }else{
				label.removeClass("label-success").addClass("label-danger");
   			label.text("否");
		    }
			meThis.checked = value;
			me.prop("checked",value);
			me.val(value);
 		};
 		this.open = function(option){
 			option = $.extend( true,  {},  option || {}); 
 			if(!option.scripts || !option.scripts instanceof Array){
 				option.scripts = _this.setting.scripts;
 			}
 			if(!option.setting){
 				option.setting = _this.setting;
 			}
 			return _this.getLayer().open(option);
 		}
		this.toAdd = function(option){
		     var option = option || {};
			 var para = {};
			 if(option && option.hasOwnProperty("para")){
				 para = $.extend( true,  {}, para, option.para || {}); 
   		  	  }
			 var _toAddUrl = _this.setting.toAddUrl;
   		  	 if(option && option.toAddUrl){
   		  		_toAddUrl = option.toAddUrl;
			  }
			 _this.get(para,{
				 url:_toAddUrl,
				 success:function(result,para){
					    var data = result.data;
	    				var _title = "新增";
	    				if(option && option.title){
	    					_title = option.title;
	    				}
	    				_title = _title + _this.modelName ;
	    				var _addDivId = _this.setting.addDivId;
	    				if(option && option.addDivId){
	    					_addDivId = option.addDivId;
	    				}
	    				var w = '500px' , h='';
	    				if(option && option.width){
	    					w = option.width;
	    				}
	    				if(option && option.height){
	    					h = option.height;
	    				}
	    				_this.open({
	    					 content:$("#"+_addDivId).html()
	    					 ,title:_title
	    					 ,btn: ['保存','重置','取消']
	    				     ,area: [w, h]
	    				     ,id:_addDivId+"FormDialog"
	    				     ,success: function(layero, index){
	    				    	 if(_this.formInit){
	    					    	_this.formInit(layero,index,"add");
	    					     }
	    				    	 layero.find("form").validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
	    				    	 if(data){
	    				    		for ( var name in data ){ // 方法
	  		  	    					 var value = data[name];
	  		  	    					 if(value==null){
	  		  	    						value = "";
	  		  	    					 }
	  		  	    					_this.renderAddPage(layero,data,name,value);
	  		  	    				 }
	    				    	 }else{
	    				    		 tvc.changeDefaultValues(layero.find("input"));
	    				    	 }
		  	    				_this.renderAddPageOther(layero,data,result.para,option);
	    				     }
	    				     ,yes: function(index, layero){
	    				    	 var form = layero.find("form");  
	    				    	 if(form.validationEngine("validate")){
	    				    		 if(option && option.checkForm){
	    				    			if(!option.checkForm(layero)){
	    				    				return false;
	    				    			}
	    				    		 }
	    				    		 var para = form.formSerializeNotBlank();  
	    				    		 var msg = "确定保存？";
	    				    		 var _option = {
	  	  		  	    					 msg:msg
										    ,index:index
	  	  		  	    					,success:function(result,para){
		  	    				    			_this.addItemAfter(result,para);
		  	    				    		 }
	  	  		  	    				     ,closeOther:false
	  	  		  	    				 }
	  	  		  	    			_option = $.extend( true,  {}, _option, option || {}); 
	    				    		 _this.addItem(para,_option);
	    				    	 }
	    				      }
	    				      ,btn2: function(index, layero){
	    				    	  tvc.resetValues(layero.find("input"));
	    				    	  _this.resetAfterForDialog(layero);
	    				    	  return false;
	    					  } 
	    				      ,btn3: function(index, layero){
  	    				    	  layero.find("form").find(".editForm-msg").parent("div").removeClass("hide").addClass("hide");
  	    				    	  layero.find("form").find(".editForm-msg").text("");
  	    				    	   if(option.cancel){
	  	    						   if(option.cancel(para)){
	  	    							   _this.getLayer().close(index);
	  	    						   }
	  	    					   }else{
	  	    						 _this.getLayer().close(index);
	  	    					   }
  	    				      } 
  	    					  ,cancel: function(){ 
	  	    					   if(option.cancel){
	  	    						   return option.cancel(para);
	  	    					   }
  	    					  }
	    				 });
				 }
			 })
	    };
	    this.openDialog = function(option){
	    	var w = '500px' , h='';
			if(option && option.width){
				w = option.width;
			}
			if(option && option.height){
				h = option.height;
			}
	    	_this.open({
				 content:$("#"+option.targetId).html()
				 ,title:option.title
				 ,btn: ['提交','重置','取消']
			     ,area: [w, h]
			     ,id:option.targetId+"FormDialog"
			     ,success: function(layero, index){
			    	 if(_this.formInit){
				    	_this.formInit(layero,index,"open");
				     }
			    	 layero.find("form").validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
			    	 var data = option.data;
			    	 if(data){
			    	 }else{
			    		 tvc.changeDefaultValues(layero.find("input"));
			    	 }
			     }
			     ,yes: function(index, layero){
			    	 var form = layero.find("form");  
			    	 if(form.validationEngine("validate")){
			    		 if(option && option.checkForm){
			    			if(!option.checkForm(layero)){
			    				return false;
			    			}
			    		 }
			    		 var para = form.formSerializeNotBlank();  
			    		 var msg = "确定提交？";
			    		 var _option = {
		  	    					 msg:msg
							        ,index:index
		  	    					,success:function(result,para){
	    				    		 }
		  	    				     ,closeOther:false
		  	    				 }
			    		 var successMsg = '提交成功！';
				  		  if(option && option.successMsg){
				  			successMsg = option.successMsg;
				  		  }
				  		_option.successMsg =  successMsg;
		  	    			_option = $.extend( true,  {}, _option, option || {}); 
			    		 _this.addItem(para,_option);
			    	 }
			      }
			      ,btn2: function(index, layero){
			    	  tvc.resetValues(layero.find("input"));
			    	  _this.resetAfterForDialog(layero);
			    	  return false;
				  } 
			      ,btn3: function(index, layero){
				    	  layero.find("form").find(".editForm-msg").parent("div").removeClass("hide").addClass("hide");
				    	  layero.find("form").find(".editForm-msg").text("");
				    	   if(option.cancel){
 						   if(option.cancel(para)){
 							   _this.getLayer().close(index);
 						   }
 					   }else{
 						 _this.getLayer().close(index);
 					   }
				      } 
					  ,cancel: function(){ 
 					   if(option.cancel){
 						   return option.cancel(para);
 					   }
					  }
			 });
	    }
  		this.toUpdate = function(id,option){
  		     var option = option || {};
	    	  var para = {id:id};
	    	  var _toUpdateUrl = _this.setting.toUpdateUrl;
    		  if(option && option.toUpdateUrl){
    			  _toUpdateUrl = option.toUpdateUrl;
			  }
    		  if(option && option.hasOwnProperty("para")){
    			  para = $.extend( true,  {}, para, option.para || {}); 
    		  }
    		  _this.get(para,{
    			  url:_toUpdateUrl,
    			  failMsg:'获取数据失败！',
    			  closeOther:false,
    			  success:function(result,para){
    				    var data = result.data; 
	  	    			if(data){
	  	    				var _title = "修改";
	  	    				if(option && option.title){
	  	    					_title = option.title;
	  	    				}
	  	    				_title = _title+_this.modelName;
	  	    				var _editDivId = _this.setting.editDivId;
	  	    				if(option && option.editDivId){
	  	    					_editDivId = option.editDivId;
	  	    				}
	  	    				var w = '500px' , h='';
	  	    				if(option && option.width){
	  	    					w = option.width;
	  	    				}
	  	    				if(option && option.height){
	  	    					h = option.height;
	  	    				}
	  	    				if(option && option.label){
	  	    					_title = _title +"【"+option.label+"】";
	  	    				}
	  	    				_this.open({
	  	    					 content:$("#"+_editDivId).html()
	  	    					 ,title:_title
	  	    					 ,btn: ['保存','重置','取消']
	  	    				     ,area: [w, h]
	  	    				     ,id:_editDivId+"FormDialog"
	  	    				     ,success: function(layero, index){
	  	    				    	if(_this.formInit){
	  	    					    	_this.formInit(layero,index,"edit");
	  	    					     }
	  	    				    	 layero.find("form").validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
	  		  	    				  if(data){
		  		  	    				 for ( var name in data ){ // 方法
		  		  	    					 var value = data[name];
		  		  	    					 if(value==null){
		  		  	    						value = "";
		  		  	    					 }
		  		  	    					_this.renderUpdatePage(layero,data,name,value);
		  		  	    				 }
	  		  	    				 }
		  		  	    			_this.renderUpdatePageOther(layero,data,result.para,option);
	  	    				     }
	  	    				     ,yes: function(index, layero){
	  	    				    	 var form = layero.find("form");
	  	    				    	 if(form.validationEngine("validate")){
	  	    				    		 if(option && option.checkForm){
	  	    				    			if(!option.checkForm(layero)){
	  	    				    				return false;
	  	    				    			}
	  	    				    		 }
  	    				    			 var para = form.formDataForUpdate();
	  	    				    		 if(form.formUpdated()){
	  	    				    			 var para = form.formDataForUpdate();
	  	    				    			 var msg = "确定保存？";
		  	  		  	    				 var _option = {
		  	  		  	    					 msg:msg
												 ,index:index
		  	  		  	    					,success:function(result,para){
			  	    				    			    _this.updateItemAfter(result,para);
			  	    				    		 }
		  	  		  	    				     ,closeOther:false
		  	  		  	    				 }
		  	  		  	    				 _option = $.extend( true,  {}, _option, option || {}); 
		  	    				    		 _this.updateItem(para,_option);
	  	    				    		 }else{
	  	    				    			 _this.remind('没有数据被修改，不必保存！', {closeOther:false});
	  	    				    		 }
	  	    				    	 }
	  	    				      }
	  	    				      ,btn2: function(index, layero){
	  	    				    	tvc.resetValues(layero.find("input"));
	  	    				    	layero.find("form").resetForm();
	  	    				    	_this.resetAfterForDialog(layero);
	  	    				    	return false;
	  	    					  } 
	  	    				      ,btn3: function(index, layero){
	  	    				    	  layero.find("form").find(".editForm-msg").parent("div").removeClass("hide").addClass("hide");
	  	    				    	  layero.find("form").find(".editForm-msg").text("");
	  	    				    	   if(option.cancel){
		  	    						   if(option.cancel(para)){
		  	    							   _this.getLayer().close(index);
		  	    						   }
		  	    					   }else{
		  	    						 _this.getLayer().close(index);
		  	    					   }
	  	    				      } 
	  	    					  ,cancel: function(){ 
		  	    					   if(option.cancel){
		  	    						   return option.cancel(para);
		  	    					   }
	  	    					  }
	  	    				 });
	  	    			}
    			  }
    		  })
	    };
	    this.toDetail = function(id,option){ 
		      var option = option || {};
	    	  var para = {id:id};
	    	  var _toDetailUrl = _this.setting.toDetailUrl;
    		  if(option && option.toDetailUrl){
    			  _toDetailUrl = option.toDetailUrl;
			  }
    		  if(option && option.hasOwnProperty("para")){
    			  para = $.extend( true,  {}, para, option.para || {}); 
    		  }
    		  _this.get(para,{
    			  url:_toDetailUrl,
    			  failMsg:'获取数据失败！',
    			  closeOther:false,
    			  success:function(result,para){
    				  var data = result.data;
	  	    			if(data){
	  	    				var _title = "详情";
	  	    				if(option && option.title){
	  	    					_title = option.title;
	  	    				}
	  	    				var _detailDivId = _this.setting.detailDivId;
	  	    				if(option && option.detailDivId){
	  	    					_detailDivId = option.detailDivId;
	  	    				}
	  	    				var w = '500px' , h='';
	  	    				if(option && option.width){
	  	    					w = option.width;
	  	    				}
	  	    				if(option && option.height){
	  	    					h = option.height;
	  	    				}
	  	    				if(option && option.label){
	  	    					_title = _this.modelName +"【"+option.label+"】"+_title;
	  	    				}
	  	    				_this.open({
	  	    					 content:$("#"+_detailDivId).html()
	  	    					 ,title:_title
	  	    				     ,area: [w, h]
	  	    				     ,id:_detailDivId+"FormDialog"
	  	    				     ,success: function(layero, index){
	  	    				    	 if(data){
		  		  	    				 for ( var name in data ){ // 方法
		  		  	    					 var value = data[name];
		  		  	    					 if(value==null){
		  		  	    						value = "";
		  		  	    					 }
		  		  	    					_this.renderDetailPage(layero,data,name,value);
		  		  	    				 }
	  		  	    				 }
	  		  	    				_this.renderDetailPageOther(layero,data,result.para,option);
	  	    				     }
	  	    				     ,yes: function(index, layero){
	  	    				    	   if(option.cancel){
		  	    						   if(option.cancel(para)){
		  	    							   _this.getLayer().close(index);
		  	    						   }
		  	    					   }else{
		  	    						 _this.getLayer().close(index);
		  	    					   }
	  	    				      } 
	  	    					  ,cancel: function(){ 
		  	    					   if(option.cancel){
		  	    						   return option.cancel(para);
		  	    					   }
	  	    					  }
	  	    				 });
	  	    			}
    			  }
    		  });
	    };
	    this.removeTableTr = function(id){
  		 	 var targetIndex = parseInt($("#"+id).attr("index"));
  		 	 $("#"+id).remove();
	    	 $('#'+_this.setting.tableId+" tr").each(function(){
	    		 var trObj = $(this);
	    		 var index = parseInt(trObj.attr("index"));
	    		 if(index > targetIndex){
	    			 trObj.attr("index",--index);
	    			 var td = $(this).find("td").get(_this.setting.indexTdindex)
	    		     $(td).text(trObj.attr("index"));
	    		 }
	    	 })
	    };
	    this.controllerInit = function($ele,defaultValue){
	    	$ele.each(function(){
				var value = $(this).val();
				var dfault = true;//默认是打开
				if(defaultValue!=undefined){
					dfault = defaultValue;
				}
			    if(value!="true"){
			    	this.checked = !dfault;
			    	$(this).prop("checked",!dfault);
			    }else{
			    	this.checked = dfault;
			    	$(this).prop("checked",dfault);
			    }
			});
	    };
	    this.toString = function(obj){
	    	return JSON.stringify(obj); 
	    }
};  